//@author A0087294-unused
//Due to usage of String^ which has compilation error 
//for integration unit testing. Sorry.

/**
  * FileIO supports reading and writing to the file TExemplar.data
  * 
  * Interface functions include:
  * 1. FileIO()
  * 2. getVector()
  * 3. performReadWrite()	
  * 
  * Upon initialising, the constructor FileIO() will check if the .data file
  * exists. If not, it creates one (for first time users). Then, the constructor
  * will read from the .data file and store in vector<Event*>* eventList.
  *
  * getVector() returns a pointer to the vector eventList in the form vector<Event*>* 
  *
  * performReadWrite(string operationType) performs either read or write depending on the 
  * parameter operationType supplied. Specify "read" for a read operation and "write"
  * for a write operation.
  *
  * Coded by Calvin Misson~
  *
  **/

#pragma once

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <msclr\marshal_cppstd.h>

//#include "OperationType.h"
#include "TECommonFunctions.h"
#include "CurrentTime.h"
#include "Event.h"
#include "EventTime.h"

#include "logging.h"
using namespace ::logging;

using namespace GetCurrentTimeFunctions;
using namespace CommonDataType;
using namespace System;
using namespace System::IO;
using namespace msclr::interop;


class FileIO
{
	private:

		static vector<Event*>* eventList;
        static const std::string fileInUse; 
		
	public:

		static void performReadWrite(string operationType);
		std::vector<Event*>* FileIO::getVector();
		FileIO();

		static string getLine(int index);

	protected:

		static const string OPERATION_READ;
		static const string OPERATION_WRITE; 

		//supported operations
		static void initialiseFile();
		static void readFromFile();
		static void writeToFile();
		
		//for initialising function
		static void createNewFile(String^ nameOfFile);

		//for reading function
		static void analyzeData(string data);
		static void storeToVector(std::vector<string>* taskDetails);
		static TimeType determineTimeType(int index);
		static void parseTaskInfo (string data, std::vector<string>* taskDetails);
		static string parseTaskDetails(string data, std::vector<string>* task);
		static string parseTaskStartTime(string data, std::vector<string>*  task);
		static void parseTaskEndTime(string data, std::vector<string>* task);
		static Time* setEndTime(std::vector<string>* taskDetails);
		static Time* setStartTime(std::vector<string>* taskDetails);

		//for writing function
		static string makeIntoString(int index);
		static string storeTime(int index, string eventAndTime);
		static void addToFile(vector<string>* stringOfEvents);
		static void addContentToFile(vector<string>* stringOfEvents);

		//complementary functions
		static String^ getFileName();
		static string convertToString(int index);
		static string convertToString(String^ toBeConverted);
		static string convertToString(CommonDataType::Byte byteVariable);
		static CommonDataType::Byte convertToByte(string toBeConverted);

		static bool checkFileExists(String^ fileName);
};
